//define(function(require,exports,module){
    angular.module('shopApp')
        .controller('CateCtrl',["$rootScope","$scope","$http","$routeParams", function ($rootScope,$scope,$http,$routeParams) {
            $scope.shopId = $routeParams.shopId;
            $scope.hasDish = false;

            $rootScope.pagetitle= "店铺";
            $rootScope.next = '<a href="/res/static/index.html#/auth" target="_self"><i class="icon-font" title="我的">&#xe61e;</i></a>';
            //区域
            $scope.areaCode=$routeParams.areaCode?$routeParams.areaCode:0;
            $scope.area=$routeParams.area?$routeParams.area:'区域';
            $scope.hasArea = false;
            $http.get(hpfbase.url+"/u/user/get-region.jhtm")
                .success(function(ret){
                    if(ret.statusCode==1){
                        $scope.areaList = ret.data;
                        if ($scope.areaList) {
                            $scope.hasArea = true;
                        };
                    }
                });

            //类型
            $scope.typeIds=$routeParams.typeIds?$routeParams.typeIds:0;
            $scope.types=$routeParams.types?$routeParams.types:'分类';
            $scope.hasTypes = false;
            $http.get(hpfbase.url+"/u/shop/get-shops-type.jhtm")
                .success(function(ret){
                    if(ret.statusCode){
                        $scope.typesList = ret.data;
                        if ($scope.typesList) {
                            $scope.hasTypes = true;
                        };
                    }
                });

            //排序
            $scope.shopSorter=$routeParams.shopSorter?$routeParams.shopSorter:0;
            $scope.sorts=$routeParams.sorts?$routeParams.sorts:'排序';
            $scope.sortList = [
                {name:"DEFAULT",zh:"综合排序",val:""},
                {name:"BOOKING",zh:"预订优先",val:""},
                {name:"PREFER",zh:"优惠优先",val:""},
                {name:"DISTANCE",zh:"距离优先",val:""},
                {name:"SALES",zh:"销量优先",val:""}];
            //当前排序的名称
            function getSortName(){
                var _sorts= "";
                angular.forEach($scope.sortList,function(value){
                    if(value.name == $scope.shopSorter){
                        _sorts = value.zh;
                    }
                });
                return _sorts;
            }
            $scope.sorts = getSortName();
            if ($scope.sortList) {
                $scope.hasSort = true;
            }

            //取地理位置
            !function getLocation()
            {
                if (navigator.geolocation)
                {
                    navigator.geolocation.getCurrentPosition(showPosition,showError);
                }

            }();
            
            function showPosition(position)
            {
                var latitude= position.coords.latitude;
                var longitude=position.coords.longitude;
                $scope.location=latitude+","+longitude;
                //取到地理位置后
                getShopList();
            }
            function showError(error)
            {
                var innerHTML;
                switch(error.code)
                {
                    case error.PERMISSION_DENIED:
                        innerHTML="User denied the request for Geolocation."
                        break;
                    case error.POSITION_UNAVAILABLE:
                        innerHTML="Location information is unavailable."
                        break;
                    case error.TIMEOUT:
                        innerHTML="The request to get user location timed out."
                        break;
                    case error.UNKNOWN_ERROR:
                        innerHTML="An unknown error occurred."
                        break;
                }
                getShopList();
            }
            console.log($scope.location);
            
            
            //shop列表
            function getShopList(){
                $scope.pageNum =1;
                $http.get(hpfbase.url+"/u/shop/search-shop-list.jhtm",{
                    areaCode:$scope.areaCode,
                    typeIds:$scope.typeIds,
                    shopSorter:$scope.shopSorter,
                    location:$scope.location,
                    pageNum:$scope.pageNum,
                    numPerPage:10
                })
                    .success(function(ret){
                        if(ret.statusCode){
                            $scope.shophList = ret.data.dataList;
                            if ($scope.shophList) {
                                $scope.hasShop = true;
                            };
                        }
                    });
            }

            //加载分页内容
            $scope.showMore = function() {
                $scope.pageNum = $scope.pageNum++;
                $http.get(hpfbase.url+"/u/shop/search-shop-list.jhtm",{
                    areaCode:$scope.areaCode,
                    typeIds:$scope.typeIds,
                    shopSorter:$scope.shopSorter,
                    location:$scope.location,
                    pageNum:$scope.pageNum,
                    numPerPage:10
                })
                .success(function(ret){
                    if($scope.pageNum>=ret.totalPage){
                        return false;
                    }
                    if(ret.statusCode){
                        //追加数据
                        for(var i=0; i<ret.data.dataList.length;i++){
                            $scope.shophList.push(ret.data.dataList[i]);
                        }
                    }
                });
            };
        }]);
//})
